/**
 * Created by Administrator on 2016/6/13.
 */

   // var serviceHttp = 'http://intelab.ilabservice.cloud/';
var serviceHttp = 'http://inmycars.ihengtian.top:8998/';
//  var serviceHttp = 'http://192.168.1.72:8998/';


/*userLogin--用户登录*/
var userLogin = angular.module('loginApp', [], function () {
});

var loginController = function ($http, $scope) {

    var userName = $("#userName").val();
    var userPwd = $("#userPwd").val();
    var userNameValue = getCookieValue("userName");
    $scope.userName = userNameValue;
    var passwordValue = getCookieValue("userPwd");
    $scope.userPwd = passwordValue;
    var saveCookieValue = getCookieValue("saveCookie");
    if( userNameValue && passwordValue){
        if(saveCookieValue){
            $("#saveCookie").attr('checked', saveCookieValue);
        } else {
            $("#saveCookie").removeAttr('checked');
        }
    }else{
        $("#saveCookie").removeAttr('checked');
    }

    //保存cookie
    $('#saveCookie').click(function(){
        if($("#saveCookie").attr('checked')){
            setCookie("saveCookie",$("#saveCookie").attr('checked'),24,"/");
        } else {
            setCookie("saveCookie",'',24,"/");
        }
    });


    //点击回车键登录
    $scope.myKeyup = function(e){
        var keycode = window.event?e.keyCode:e.which;
        if(keycode==13){
            $scope.Login();
        }
    };

    var companyUrl = location.search.substr(1).split("=")[0];  //"company"（K）
    var companyJC = location.search.substr(1).split("=")[1]; //"company"（V）
    //获取当前企业名称
    var companyName = "";

    //显示企业logo
    $scope.showCompanyLogo = function () {
        function GetRequest() {
            var url = location.search;
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        var Req = new Object();
        Req = GetRequest();
        companyName =  Req["company"];
        if(companyUrl && companyName ){
            $scope.hasCompany = 1;
            $http.get(serviceHttp + 'api/rest/firm/query/login/company?companyId=' + companyName).success(function (res) {
                $scope.getData = res.data;
                $scope.companyLogo = $scope.getData.logo;
            });
        }else {
            $scope.hasCompany = 0;
        }

    }
    $scope.showCompanyLogo();

    //判定用户是否登录
    $scope.IsLogin = function () {
        //success,用户已经登录；error，用户未登录
        $http.get(serviceHttp + 'api/rest/operate/is/login').success(function (res) {
            window.location.href = "index.html";
        }).error(function (res) {

        });
    }
    $scope.IsLogin();
    //登录
    $scope.Login = function () {
        var userName = $scope.userName;
        var userPwd = $scope.userPwd;

        function GetRequest() {
            var url = location.search;
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        var Req = new Object();
        Req = GetRequest();
        //平台账号company等于空
        if( Req["company"] == undefined){
            Req["company"] = "";
        }
        companyName = Req["company"]; //E(V)

        if ($scope.userform.userName.$invalid && $scope.userform.userPwd.$valid) {
            $scope.error = {
                success: false,
                message: "请输入正确的帐号"
            }
        }
        if ($scope.userform.userName.$valid && $scope.userform.userPwd.$invalid) {
            $scope.error = {
                success: false,
                message: "请输入正确的密码"
            }
        }
        if ($scope.userform.userName.$invalid && $scope.userform.userPwd.$invalid) {
            $scope.error = {
                success: false,
                message: "输入不合法，请正确输入！"
            }
        }
        if ($scope.userform.userName.$valid && $scope.userform.userPwd.$valid) {

            $http.get(serviceHttp + 'api/rest/login?name=' + userName + '&verify=' + userPwd +'&company=' + companyName).success(function (res) {
               console.log(companyName);

                if( $("#saveCookie").attr('checked')){
                    setCookie("userName",$scope.userName,24,"/");
                    setCookie("userPwd", $scope.userPwd,24,"/");
                } else {
                    deleteCookie("userName","/");
                    deleteCookie("userPwd","/");
                }
                
                window.location.href = "index.html";
                $scope.username = userName;

            }).error(function () {
                $scope.error = {
                    message: "验证失败"
                }
            });
        }
    }

    $scope.redirect = function (url) {
        $location.url(url);
    }
    //忘记密码
    $scope.forgetPassword = function () {
      window.location.href = "forgetPassword.html?companyJC=" + companyJC;
    }

}

